package org.sundatagroup.aurora.show.entity.model;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.sundatagroup.aurora.show.entity.asset.Asset;

import java.io.Serializable;

/**
 * <p>资源 {@link Asset} 在数据库的数据结构
 * </p>
 * 
 * @author CharlesChen
 * @date 2018-04-24
 * @since 0.1.1
 */
@Document(collection = "asset")
public class AssetModel implements Serializable{
    private static final long serialVersionUID = -6596894307752913267L;

    @Id
    private int aid;
    private String name;
    private String desc;
    private String type;
    private String data;

    /**
     * data字段从String型转换为JsonObject型。
     * @return
     */
    public JsonObject getDataToJson() {
    	if (data == null) {
    		return null;
    	}
        JsonParser jsonParser = new JsonParser();
        return jsonParser.parse(data).getAsJsonObject();
    }

    /**
     * 设置data字段，主要是Asset转换为AssetModel时调用。
     * @param assetData {@link Asset}的data字段（JsonObj型）
     */
    public String setData(JsonObject assetData) {
        return assetData == null ? null : assetData.toString();
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AssetModel that = (AssetModel) o;

        if (aid != that.aid) return false;
        if (!name.equals(that.name)) return false;
        if (desc != null ? !desc.equals(that.desc) : that.desc != null) return false;
        if (!type.equals(that.type)) return false;
        return data != null ? data.equals(that.data) : that.data == null;
    }

    @Override
    public int hashCode() {
        int result = aid;
        result = 31 * result + name.hashCode();
        result = 31 * result + (desc != null ? desc.hashCode() : 0);
        result = 31 * result + type.hashCode();
        result = 31 * result + (data != null ? data.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "AssetModel{" +
                "aid=" + aid +
                ", name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                ", type='" + type + '\'' +
                ", data=" + data +
                '}';
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getData() {
        return data;
    }
}
